package com.dailyblue.project.smbms.alibaba.user.controller;

import com.dailyblue.project.smbms.alibaba.commons.bean.User;
import com.dailyblue.project.smbms.alibaba.commons.util.JsonResult;
import com.dailyblue.project.smbms.alibaba.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/{page}")
    public JsonResult find(@PathVariable("page") int page) {
        return userService.find(page);
    }

    @DeleteMapping("/{id}/{page}")
    public JsonResult delete(@PathVariable("id") int id, @PathVariable("page") int page) {
        return userService.delete(id, page);
    }

    @GetMapping("/id/{id}")
    public JsonResult id(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @GetMapping("/search/{name}")
    public JsonResult findByName(@PathVariable("name") String name, @RequestParam(name = "page", defaultValue = "1") int page) {
        return userService.findByName(name, page);
    }

    @PostMapping
    public JsonResult save(User user, int lastPage, int total) {
        return userService.saveUser(user, lastPage, total);
    }

    @GetMapping("/phone/{phone}")
    public JsonResult checkPhone(@PathVariable String phone) {
        return userService.checkPhone(phone);
    }

    @GetMapping("/code/{code}")
    public JsonResult checkCode(@PathVariable String code) {
        return userService.checkCode(code);
    }

    @PutMapping
    public JsonResult update(User user) {
        return userService.updateUser(user);
    }

    @GetMapping("/change/{id}/{password}")
    public JsonResult update(@PathVariable("id") Integer id, @PathVariable("password") String password) {
        return userService.updateUserPassword(id, password);
    }

    @GetMapping("/oldpass/{oldPass}")
    public JsonResult oldPass(HttpServletRequest request, @PathVariable String oldPass) {
        return userService.oldPass(request, oldPass);
    }
}
